package sixth.group.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sixth.group.dao.DishesMapper;
import sixth.group.dao.RestaurantMapper;
import sixth.group.domain.Dishes;
import sixth.group.domain.DishesExample;
import sixth.group.domain.Restaurant;
import sixth.group.domain.RestaurantExample;
import sixth.group.service.RestaurantService;

import java.util.List;

//此处需要添加注解使IOC容器能够将该类的实例对象作为组件扫描到容器内
@Service(value = "restaurantService")
public class RestaurantServiceImpl implements RestaurantService {

    //自动注入:看到存在成员时如果有Autowired注解,就从IOC容器中取出对象使用
    @Autowired
    private  RestaurantMapper restaurantMapper;

    @Autowired
    private DishesMapper dishesMapper;

    @Override
    public boolean resRegnew(Restaurant restaurant) {
        //查询是否存在重名的用户名
        //mybatis生成的条件实体类的用法
        RestaurantExample restaurantExample = new RestaurantExample();
        RestaurantExample.Criteria criteria = restaurantExample.createCriteria();
        criteria.andRusernameEqualTo(restaurant.getRusername());

        List<Restaurant> restaurants = restaurantMapper.selectByExample(restaurantExample);
        if(restaurants.size() > 0){
            System.out.println("存在同名用户");
            return false;
        }
        else {
            int i = restaurantMapper.insertSelective(restaurant);
            System.out.println("成功添加了" + i + "个商家");
            return true;
        }
    }

    @Override
    public boolean resLoginCheck(Restaurant restaurant) {
        boolean flag = false;
        RestaurantExample restaurantExample = new RestaurantExample();
        RestaurantExample.Criteria criteria = restaurantExample.createCriteria();
        criteria.andRusernameEqualTo(restaurant.getRusername()).andRpassEqualTo(restaurant.getRpass());
        List<Restaurant> restaurants = restaurantMapper.selectByExample(restaurantExample);
        if(restaurants.size() > 0){
            flag = true;
        }else{
            flag = false;
        }
        return flag;
    }



    @Override
    public boolean resChangeInfo(Restaurant restaurant) {
        int i=restaurantMapper.updateByPrimaryKeySelective(restaurant);
        if(i==1)
        {
            return true;
        }else{
            return false;
        }
    }


    @Override
    public List<Dishes> checkDishes(Restaurant restaurant) {
        DishesExample dishesExample =new DishesExample();
        DishesExample.Criteria criteria = dishesExample.createCriteria();
        criteria.andDridEqualTo(restaurant.getRid());
        List<Dishes> dishes = dishesMapper.selectByExample(dishesExample);
        return dishes;
    }

    @Override
    public Restaurant selectwholeinfo(Restaurant restaurant) {
        Restaurant restaurant1 = restaurantMapper.selectByUsername(restaurant.getRusername());

        //System.out.println(restaurant.toString());

        return restaurant1;
    }

    @Override
    public List<Restaurant> getAllres() {
        List<Restaurant> restaurants=restaurantMapper.selectByExample(null);

        return restaurants;
    }


    @Override
    public boolean checkname(Restaurant restaurant) {
        //查询是否存在重名的用户名
        //mybatis生成的条件实体类的用法
        RestaurantExample restaurantExample = new RestaurantExample();
        RestaurantExample.Criteria criteria = restaurantExample.createCriteria();
        criteria.andRusernameEqualTo(restaurant.getRusername());

        List<Restaurant> restaurants = restaurantMapper.selectByExample(restaurantExample);
        if(restaurants.size() > 0){
            System.out.println("存在同名用户");
            return false;
        }
        else {
            return true;
        }
    }

    @Override
    public Restaurant selectbyrid(Integer rid) {
        Restaurant restaurant = restaurantMapper.selectByPrimaryKey(rid);
        return restaurant;
    }

}
